package com.fast.common.utils;

import org.apache.commons.lang.ClassUtils;
import org.apache.commons.lang3.StringUtils;

/**
 * @author forrest
 * @Description
 * @create 2024-07-20 15:01
 */
public class ExceptionUtils extends org.apache.commons.lang.exception.ExceptionUtils {

    /**
     * 获取异常的简单信息，如果有message则返回message，否则返回类名
     *
     * @param th 异常
     * @return
     */
    public static String getSimpleMessage(Throwable th) {
        if (th == null) {
            return "";
        }
        String clsName = ClassUtils.getShortClassName(th, null);
        String msg = th.getMessage();
        return StringUtils.defaultString(msg, clsName);
    }

    /**
     * 获取最底层的异常的简单信息，如果有message则返回message，否则返回类名
     *
     * @param th 异常
     * @return
     */
    public static String getSimpleRootCauseMessage(Throwable th) {
        Throwable root = getRootCause(th);
        root = (root == null ? th : root);
        return getSimpleMessage(root);
    }
}
